package cn.chinasyq.photoquan.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import cn.chinasyq.photoquan.R;

public class ItemButtonDialog extends Dialog implements View.OnClickListener {

    private Context mContext = null;
    private String[] mItems = null;

    public String[] getmItems() {
        return mItems;
    }

    public void setmItems(String[] mItems) {
        this.mItems = mItems;
    }

    private LinearLayout mLlytItems = null;
    private Button mBtnCancel = null;
    private OnItemClickListener mListener = null;

    public ItemButtonDialog(Context context, int theme) {
        super(context, theme);
    }

    public ItemButtonDialog(Context context, String[] items) {
        this(context, R.style.Dialog_Black_Transparent);
        this.mItems = items;
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_item_bottom);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        initView();
        addView();
    }

    private void initView() {
        mBtnCancel = (Button) findViewById(R.id.btn_cancel);
        mLlytItems = (LinearLayout) findViewById(R.id.llyt_items);
        mBtnCancel.setOnClickListener(this);
    }

    private void addView() {
        if (mItems == null) {
            return;
        }
        for (int i = 0; i < mItems.length; i++) {
            Button btn = new Button(mContext);
            btn.setTag("btn_item_" + i);
            btn.setText(mItems[i]);
            btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            btn.setTextColor(Color.BLACK);
            btn.setBackgroundResource(R.drawable.sl_dialog_btn);
            btn.setGravity(Gravity.CENTER);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, getContext().getResources().getDimensionPixelSize(R.dimen.dialog_with));
            params.setMargins(0, 0, 0, 10);
            btn.setLayoutParams(params);
            btn.setOnClickListener(this);
            mLlytItems.addView(btn);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            default:
                if (this.mListener != null) {
                    try {
                        //添加 try catch , 当 View 的 Tag 为 btn_item_ 的按钮才能执行onItemClick()
                        Button btn = (Button) v;
                        String txt = btn.getText().toString();
                        String tag = btn.getTag().toString();
                        tag = tag.replace("btn_item_", "");
                        mListener.onItemClick(v, txt,
                                Integer.parseInt(tag));
                        this.dismiss();
                    } catch (Exception e) {
                    }
                }
                break;
        }
    }

    /**
     * 点击事件监听类
     */
    public interface OnItemClickListener {
        /**
         * 选项的点击事件
         *
         * @param v     点击的 View 对象
         * @param txt   选项中显示的文本
         * @param index 在选项列表中的索引位置
         */
        void onItemClick(View v, String txt, int index);
    }

    /**
     * 设置选项点击监听器
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}
